package com.guanyong.www.controllers.api;

import com.guanyong.model.*;

import com.guanyong.services.KeybaseService;
import com.guanyong.services.MaoyanService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.www.config.HuatiUp;
import com.guanyong.www.config.MovieIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by mxk on 2017/7/16.
 */
@RestController
@RequestMapping("/api/index/")
public class IndexApi {

    @Autowired
    KeybaseService keybaseService;


    @Autowired
    MaoyanService maoyanService;

    @RequestMapping(value = "getallmovie", method = RequestMethod.GET)
    public ResultObject<List<MaoyanMovieModel>> GetAllMovies(){

        ResultObject<List<MaoyanMovieModel>> result = new ResultObject<List<MaoyanMovieModel>>();
        result.setResult(maoyanService.GetMaoyanAllList());
        result.setErrorMsg(ResultCode.OperateOk);
        return result;
    }


    @RequestMapping(value = "getmovie", method = RequestMethod.GET)
    public ResultObject<List<KeyBaseModel>> getmovie(){

        ResultObject<List<KeyBaseModel>> result = new ResultObject<List<KeyBaseModel>>();
        result.setResult(keybaseService.selectByAll());
        result.setErrorMsg(ResultCode.OperateOk);
        return result;
    }


    @RequestMapping(value = "getzhishu", method = RequestMethod.GET)
    public ResultObject<List<KeyJavaZhishuCountModel>> getzhishu(){
        ResultObject<List<KeyJavaZhishuCountModel>> result = new ResultObject<List<KeyJavaZhishuCountModel>>();
        result.setResult(keybaseService.getZhishu30());
        result.setErrorMsg(ResultCode.OperateOk);
        return result;
    }


    @RequestMapping(value = "getweibozhishu", method = RequestMethod.GET)
    public ResultObject<List<KeyWeiboZhishuCountModel>> getweibozhishu(){
        ResultObject<List<KeyWeiboZhishuCountModel>> result = new ResultObject<List<KeyWeiboZhishuCountModel>>();
        result.setResult(keybaseService.getKeyWeiboZhishuAll());
        result.setErrorMsg(ResultCode.OperateOk);
        return result;
    }

    @RequestMapping(value = "getpingfen", method = RequestMethod.GET)
    public ResultObject<List<KeyPingfenModel>> getpingfen(){
        ResultObject<List<KeyPingfenModel>> result = new ResultObject<List<KeyPingfenModel>>();
        result.setResult(keybaseService.getKeyPingfenByDay());
        result.setErrorMsg(ResultCode.OperateOk);
        return result;
    }

    @RequestMapping(value = "getbozhutop5", method = RequestMethod.GET)
    public ResultObject<List<KeyBozhuModel>> getbozhutop5(){
        ResultObject<List<KeyBozhuModel>> result = new ResultObject<List<KeyBozhuModel>>();
        result.setResult(keybaseService.getKeyBozhuTop5());
        result.setErrorMsg(ResultCode.OperateOk);
        return result;
    }


    @RequestMapping(value = "getKeyHuatiTop7", method = RequestMethod.GET)
    public ResultObject<List<KeyHuatiModel>> getKeyHuatiTop7(){
        ResultObject<List<KeyHuatiModel>> result = new ResultObject<List<KeyHuatiModel>>();
        if (HuatiUp.ifDate()){
            keybaseService.upHuati(HuatiUp.urlMD5);
        }
        result.setResult(keybaseService.getKeyHuatiTop7());
        result.setErrorMsg(ResultCode.OperateOk);
        return result;
    }


    @RequestMapping(value = "getPiaofangList", method = RequestMethod.GET)
    public ResultObject<List<KeyPiaofangDateModel>> getPiaofangList(){
        ResultObject<List<KeyPiaofangDateModel>> result = new ResultObject<List<KeyPiaofangDateModel>>();
        List<KeyPiaofangModel> oldList =keybaseService.getPiaofangList();
        List<KeyPiaofangDateModel> lsit = new ArrayList<KeyPiaofangDateModel>();
        for (KeyPiaofangModel keyPiaofangModel:oldList){
            KeyPiaofangDateModel keyPiaofangDateModel = new KeyPiaofangDateModel();
            keyPiaofangDateModel.setCurDate(keyPiaofangModel.getCurDate());
            keyPiaofangDateModel.setNum(keyPiaofangModel.getPiaofang());
            lsit.add(keyPiaofangDateModel);
        }
        KeyPiaofangModel keyPiaofangModel =keybaseService.getNowPiaofang();
        KeyPiaofangDateModel keyPiaofangDateModel = new KeyPiaofangDateModel();
        keyPiaofangDateModel.setCurDate(keyPiaofangModel.getCurDate());
        keyPiaofangDateModel.setNum(keyPiaofangModel.getPiaofang());
        lsit.add(keyPiaofangDateModel);
        result.setResult(lsit);
        result.setErrorMsg(ResultCode.OperateOk);
        return result;
    }


    @RequestMapping(value = "getPiaofangNow", method = RequestMethod.GET)
    public ResultObject<Map<String,Long>> getPiaofangNow(){
        ResultObject<Map<String,Long>> result = new ResultObject<Map<String,Long>>();
        List<KeyPiaofangModel> oldList =keybaseService.getPiaofangList();
        Map<String,Long> map = new HashMap<String, Long>();
        Long sum=0l;
        for (KeyPiaofangModel keyPiaofangModel:oldList){
            sum+=keyPiaofangModel.getPiaofang();
        }
        KeyPiaofangModel keyPiaofangModel =keybaseService.getNowPiaofang();
        map.put("now",keyPiaofangModel.getPiaofang());
        map.put("sum",sum+map.get("now"));
        result.setResult(map);
        result.setErrorMsg(ResultCode.OperateOk);
        return result;
    }

    @RequestMapping(value = "getPlayCount", method = RequestMethod.GET)
    public ResultObject<List<KeyPlayCountModel>> getPlayCount(){
        ResultObject<List<KeyPlayCountModel>> result = new ResultObject<List<KeyPlayCountModel>>();
        result.setResult(keybaseService.getPlayCount());
        result.setErrorMsg(ResultCode.OperateOk);
        return result;
    }
}
